//
//  AppDelegate.m
//  SQ_NEW
//
//  Created by 刘明 on 2017/5/10.
//  Copyright © 2017年 刘明. All rights reserved.
//

#import "AppDelegate.h"
#import "LMMainTabbarViewController.h"
#import "SQSocket.h"
#import "SQSocketSysModel.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
//    [self initializeStatusBarWithApplication:application];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = LMWhiteColor;
    [self.window makeKeyAndVisible];
    self.window.rootViewController = self.mainTabBarVC;
    [self startConnectSocket];//连接socket
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


- (void)initializeStatusBarWithApplication:(UIApplication *)application
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        application.statusBarStyle = UIStatusBarStyleLightContent; // Light content, for use on dark backgrounds
    } else {
        application.statusBarStyle = UIStatusBarStyleDefault; // Dark content, for use on light backgrounds
    }
    application.statusBarHidden = NO; // 是否隐藏状态栏
}



#pragma mark - 连接socket

- (void)startConnectSocket{
    
    SQSocket *socket = [SQSocket sharedSQSocket];
    socket.offLineStyle = SQSocketOffLineByUser;
    [socket socketDisconnect];
    [socket socketConnect];
    [socket socketDidSendMessage:[NSString stringWithFormat:@"<user>=%@=2\n",@"18410109053"]];
    
    //收到数据处理
    socket.socketRecevieDataBlock = ^(NSString *msg) {
      
        NSString *message = [[msg stringByReplacingOccurrencesOfString:@"<" withString:@""]stringByReplacingOccurrencesOfString:@">" withString:@""];

        if ([message hasPrefix:@"mydynamic"]) {
            
        }else if ([message hasPrefix:@"sendpermission"]){
            
        }else if ([message hasPrefix:@"sendsysmessage"]){
            //<sendsysmessage>/=2=sqtz=/=1=jftz=/=0=xtxx=/=0=xmtg=2017-05-02 11:37/=0=xmcy=2017-04-26 15:19
            [self.dataArray removeAllObjects];
            NSString *unreadMsgCount = @"0";
            NSMutableArray *mArray = [message componentsSeparatedByString:@"/"].mutableCopy;
            [mArray removeObjectAtIndex:0];
            for (NSString *msgStr in mArray) {
                
                NSMutableArray *tArray = [msgStr componentsSeparatedByString:@"="].mutableCopy;
                [tArray removeObjectAtIndex:0];
                
                SQSocketSysModel *model = [SQSocketSysModel new];
                model.unReadMsgCount = tArray[0];
                model.title = tArray[1];
                model.time = tArray[2];
                
                [self.dataArray addObject:model];
                //求未读消息总和
                unreadMsgCount = [NSString stringWithFormat:@"%d",unreadMsgCount.intValue + model.unReadMsgCount.intValue];
            }
            
            if (unreadMsgCount.intValue == 0){
                unreadMsgCount = nil;
            }else if (unreadMsgCount.intValue > 99) {
                unreadMsgCount = @"99+";
            }
            
            for (UITabBarItem *item in self.mainTabBarVC.tabBar.items) {
                
                if (item.tag == 10086) {
                    
                    item.badgeValue = unreadMsgCount;
                }
            }

        }
    };
    

    
}

#pragma mark - lazy

- (LMMainTabbarViewController *)mainTabBarVC{
    
    if (!_mainTabBarVC) {
        
        _mainTabBarVC = [[LMMainTabbarViewController alloc] init];
    }
    return _mainTabBarVC;
}

- (SQMessageViewController *)msgVC{
    
    if (!_msgVC) {
        _msgVC = [[SQMessageViewController alloc] init];
    }
    return _msgVC;
}


- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _dataArray;
}

@end
